﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;

namespace DotNet.Utilities
{
    public class ImageUtil
    {
        public enum ImageMode { WidthHeight, Width, Height, Cut };
        public enum WaterPos { TopLeft, TopRight, BottomLeft, BottomRight };

        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="imgPath">原图路径</param>
        /// <param name="newPath">缩略图路径</param>
        /// <param name="width">缩略图宽度</param>
        /// <returns></returns>
        public static void MakeThumbnail(string imgPath, string newPath, int width)
        {
            MakeThumbnail(imgPath, null, newPath, width, 0, ImageMode.Width);
        }
        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="imgPath">源图路径</param>
        /// <param name="newPath">缩略图路径</param>
        /// <param name="width">缩略图宽度</param>
        /// <param name="height">缩略图高度</param>
        /// <param name="mode">生成缩略图的方式</param>
        public static void MakeThumbnail(string imgPath, string newPath, int width, int height, ImageMode mode)
        {
            MakeThumbnail(imgPath, null, newPath, width, height, mode);
        }
        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="imgStream">图片流</param>
        /// <param name="newPath">缩略图路径</param>
        /// <param name="width">缩略图宽度</param>
        public static void MakeThumbnail(Stream imgStream, string newPath, int width)
        {
            MakeThumbnail("", imgStream, newPath, width, 0, ImageMode.Width);
        }
        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="imgStream">图片流</param>
        /// <param name="newPath">缩略图路径</param>
        /// <param name="width">缩略图宽度</param>
        /// <param name="height">缩略图高度</param>
        /// <param name="mode">生成缩略图的方式</param>
        public static void MakeThumbnail(Stream imgStream, string newPath, int width, int height, ImageMode mode)
        {
            MakeThumbnail("", imgStream, newPath, width, height, mode);
        }
        ///<summary>
        ///生成缩略图
        ///</summary>
        ///<param name="imgPath">源图路径（物理路径）</param>
        ///<param name="imgStream">源图数据流（跟路径or的关系）</param>
        ///<param name="newPath">缩略图路径（物理路径）</param>
        ///<param name="width">缩略图宽度</param>
        ///<param name="height">缩略图高度</param>
        ///<param name="mode">生成缩略图的方式</param>
        private static void MakeThumbnail(string imgPath, Stream imgStream, string newPath, int width, int height, ImageMode mode)
        {
            Image originalImage = null;
            try
            {
                originalImage = imgStream != null ? Image.FromStream(imgStream) : Image.FromFile(imgPath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (originalImage == null) return;

            int towidth = width;
            int toheight = height;

            int x = 0;
            int y = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;

            switch (mode)
            {
                case ImageMode.WidthHeight:
                    break;
                case ImageMode.Width:
                    toheight = originalImage.Height * width / originalImage.Width;
                    break;
                case ImageMode.Height:
                    towidth = originalImage.Width * height / originalImage.Height;
                    break;
                case ImageMode.Cut:              
                    if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                    {
                        oh = originalImage.Height;
                        ow = originalImage.Height * towidth / toheight;
                        y = 0;
                        x = (originalImage.Width - ow) / 2;
                    }
                    else
                    {
                        ow = originalImage.Width;
                        oh = originalImage.Width * height / towidth;
                        x = 0;
                        y = (originalImage.Height - oh) / 2;
                    }
                    break;
                default:
                    break;
            }

            Image bitmap = new Bitmap(towidth, toheight);

            Graphics g = Graphics.FromImage(bitmap);
            g.InterpolationMode = InterpolationMode.High;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.Clear(Color.Transparent);

            g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
                new Rectangle(x, y, ow, oh),
                GraphicsUnit.Pixel);

            try
            {
                bitmap.Save(newPath, ImageFormat.Jpeg);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                originalImage.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }
        }

        /// <summary>
        /// 在图片上增加文字水印
        /// </summary>
        /// <param name="imgPath">图片路径</param>
        /// <param name="newPath">添加水印后路径</param>
        /// <param name="addText">水印文字</param>
        public static void AddWaterMark(string imgPath, string newPath, string addText)
        {
            AddWaterMark(imgPath, newPath, addText, new Font("Verdana", 60), Color.Green, WaterPos.BottomRight);
        }
        /// <summary>
        /// 在图片上增加文字水印
        /// </summary>
        /// <param name="imgPath">图片路径</param>
        /// <param name="newPath">添加水印后路径</param>
        /// <param name="addText">水印文字</param>
        /// <param name="f">字体</param>
        /// <param name="c">颜色</param>
        /// <param name="pos">绘制位置</param>
        public static void AddWaterMark(string imgPath, string newPath, string addText, Font f, Color c, WaterPos pos)
        {
            Image image = Image.FromFile(imgPath);

            int width = image.Width;
            int height = image.Height;

            Graphics g = Graphics.FromImage(image);
            g.DrawImage(image, 0, 0, width, height);
            
            Brush b = new SolidBrush(c);

            int top, left;
            switch (pos)
            {
                case WaterPos.TopLeft:
                    top = 20;
                    left = 20;
                    break;
                case WaterPos.TopRight:
                    top = 20;
                    left = width - Convert.ToInt32(f.Size) * addText.Length;
                    break;
                case WaterPos.BottomLeft:
                    top = height - Convert.ToInt32(f.Size);
                    left = 20;
                    break;
                case WaterPos.BottomRight:
                    top = height - Convert.ToInt32(f.Size);
                    left = width - Convert.ToInt32(f.Size) * addText.Length;
                    break;
                default:
                    top = 20;
                    left = 20;
                    break;
            }

            g.DrawString(addText, f, b, left, top);
            g.Dispose();

            image.Save(newPath);
            image.Dispose();
        }

        ///<summary>
        /// 在图片上生成图片水印
        ///</summary>
        ///<param name="imgPath">原服务器图片路径</param>
        ///<param name="newPath">生成的带图片水印的图片路径</param>
        ///<param name="picPath">水印图片路径</param>
        public static void AddWaterPic(string imgPath, string newPath, string picPath)
        {
            Image image = Image.FromFile(imgPath);

            Image copyImage = Image.FromFile(picPath);

            Graphics g = Graphics.FromImage(image);

            Rectangle rect = new Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height);
            
            g.DrawImage(copyImage, rect, 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
            g.Dispose();

            image.Save(newPath);
            image.Dispose();
        }
    }
}
